package com.bridgeintelligent.tag.bulkload.service.core;

import com.bridgeintelligent.tag.remotefile.RemoteFileHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * @description
 * @author: grp 2021-04-06 13:41
 **/
@Service
@Slf4j
@AllArgsConstructor
public class FileService {

    private RemoteFileHandler remoteFileHandler;

    public synchronized void writeLog(String dimId,String id, String value){
        String remoteFile = remoteFileHandler.getParentPath(dimId);
        try (BufferedWriter errorWriter =
                     new BufferedWriter(new OutputStreamWriter(new FileOutputStream(remoteFile+dimId+".err",true)))) {
            errorWriter.write(id+"|"+value);
            errorWriter.newLine();
            errorWriter.flush();
        } catch (IOException e) {
            log.info("导出标签数据出错{}-{}",id,value);
            log.error("导出标签数据出错",e);
        }
    }
}
